/****************************************************************************
 * 2024.1 超萌大脑斧
 ****************************************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using UnityEngine.U2D;

namespace QFramework.Example
{
	public partial class UnlockedIconPanel : UIElement,IController
	{
		private Dictionary<string, System.Tuple<ExpUpgradeItem, Image>> mUnlockedKeys =
			new Dictionary<string, System.Tuple<ExpUpgradeItem, Image>>();

		private ResLoader mResLoader = ResLoader.Allocate();
		
		private void Awake()
		{
			UnlockedIconTemplete.Hide();
			var iconAtlas = mResLoader.LoadSync<SpriteAtlas>("Icon");// 获取icon图集

			foreach (var expUpgradeItem in this.GetSystem<ExpUpgradeSystem>().Items)
			{
				var cachedItem = expUpgradeItem;//缓存
				expUpgradeItem.CurrentLevel.RegisterWithInitValue(level =>
				{
					// 当技能等级>0时，才证明已经激活，需要展示在主界面上
					if (level>0)
					{
						if (mUnlockedKeys.ContainsKey(cachedItem.Key))
						{
							
						}
						else
						{
							// 生成图标，并设置对应的icon
							UnlockedIconTemplete.InstantiateWithParent(UnlockedIconRoot)
								.Self(self =>
								{
									self.sprite = iconAtlas.GetSprite(cachedItem.IconName);
									mUnlockedKeys.Add(cachedItem.Key,
										new System.Tuple<ExpUpgradeItem, Image>(cachedItem, self));
								})
								.Show();
						}
					}
				}).UnRegisterWhenGameObjectDestroyed(gameObject);
			}

			Global.SuperKnife.Register(unlocked =>
			{
				if (unlocked)
				{
					var key = "simple_knife";
					if (mUnlockedKeys.ContainsKey(key))
					{
						var item = mUnlockedKeys[key].Item1;
						var sprite = iconAtlas.GetSprite(item.PairedIconName);
						mUnlockedKeys[key].Item2.sprite = sprite;
					}
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			
			Global.SuperRotateSword.Register(unlocked =>
			{
				if (unlocked)
				{
					var key = "rotate_sword";
					if (mUnlockedKeys.ContainsKey(key))
					{
						var item = mUnlockedKeys[key].Item1;
						var sprite = iconAtlas.GetSprite(item.PairedIconName);
						mUnlockedKeys[key].Item2.sprite = sprite;
					}
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			
			Global.SuperBasketBall.Register(unlocked =>
			{
				if (unlocked)
				{
					var key = "bsaket_ball";
					if (mUnlockedKeys.ContainsKey(key))
					{
						var item = mUnlockedKeys[key].Item1;
						var sprite = iconAtlas.GetSprite(item.PairedIconName);
						mUnlockedKeys[key].Item2.sprite = sprite;
					}
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			
			Global.SuperBomb.Register(unlocked =>
			{
				if (unlocked)
				{
					var key = "simple_bomb";
					if (mUnlockedKeys.ContainsKey(key))
					{
						var item = mUnlockedKeys[key].Item1;
						var sprite = iconAtlas.GetSprite(item.PairedIconName);
						mUnlockedKeys[key].Item2.sprite = sprite;
					}
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			
			Global.SuperSword.Register(unlocked =>
			{
				if (unlocked)
				{
					var key = "simple_sword";
					if (mUnlockedKeys.ContainsKey(key))
					{
						var item = mUnlockedKeys[key].Item1;
						var sprite = iconAtlas.GetSprite(item.PairedIconName);
						mUnlockedKeys[key].Item2.sprite = sprite;
					}
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
		}

		protected override void OnBeforeDestroy()
		{
			mResLoader.Recycle2Cache();
			mResLoader = null;
		}

		public IArchitecture GetArchitecture()
		{
			return Global.Interface;
		}
	}
}